//
//  MarketMainView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/28.
//
//

#include "SystemMainView.h"
#include "LocalizedStringGetter.h"
#include "MarketMasonryView.h"
#include "MarketGoldView.h"
#include "AchievementView.h"
#include "PlayLevelRecorder.h"
#include "AchievementMenuItem.h"
#include "GoldSaveModel.h"

USING_NS_CC;

#define TASK1_LEVEL_NUM_KEY "task1_level_num"
#define TASK2_STAR_NUM_KEY "task2_star_num"
#define CELL_WIDTH    5
#define CELL_HEIGHT    10
#define TITLE_HEIGHT   40
#define TITLE_WIDTH    300
#define MENU_Y_PAD     5
#define DIAMOND_REWARD  10


typedef enum {
	MarketMenu1 = 1,
	MarketMenu2,
	MarketBack,
} MenuTag;

typedef enum {
    ZBg,
    ZMenu,
    ZTitle,
    ZMenuBack,
} ZIndex;


Scene *AchievementView::createScene()
{
    auto scene = Scene::create();
    auto layer = AchievementView::create();
    scene->addChild(layer);
    return scene;
}

bool AchievementView::init()
{
    if (!Layer::init()) {
        return false;
    }
    task1_level_num = UserDefault::getInstance()->getIntegerForKey(TASK1_LEVEL_NUM_KEY, 20);
    task2_star_num = UserDefault::getInstance()->getIntegerForKey(TASK2_STAR_NUM_KEY, 20);
    passnum = PlayLevelRecorder::getInstance()->getLevelPassNum();
    star_level_num = PlayLevelRecorder::getInstance()->getThreeStarLevelNum();
    bgInit();
    menuInit();
    titleInit();
    return true;
}

void AchievementView::menuInit()
{
	Size winsize = Director::getInstance()->getWinSize();

	auto menuItem1 = AchievementMenuItem::create("AchieveMenu.png", "AchieveMenuSelected.png","AchieveMenu.png",
			CC_CALLBACK_1(AchievementView::menuCallback, this));
	menuItem1->setTag(MarketMenu1);
	menuItem1->setAnchorPoint(Vec2::ZERO);
	menuItem1->setPosition((winsize.width-menuItem1->getContentSize().width)/2,
			               winsize.height - (menuItem1->getContentSize().height + MENU_Y_PAD)*1 - TITLE_HEIGHT);
	menuItem1->setLevelInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTaskLevel"), task1_level_num / 20)->getCString());
	menuItem1->setInfoTitle(LocalizedStringGetter::getStringByKey("AchieveTask1"));
	menuItem1->setInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTask1Dsc"), task1_level_num)->getCString());
	menuItem1->setProgressState(passnum ,task1_level_num);
	menuItem1->setRewardDiamond(DIAMOND_REWARD);

	auto menuItem2 = AchievementMenuItem::create("AchieveMenu.png", "AchieveMenuSelected.png","AchieveMenu.png",
			CC_CALLBACK_1(AchievementView::menuCallback, this));
	menuItem2->setTag(MarketMenu2);
	menuItem2->setAnchorPoint(Vec2::ZERO);
	menuItem2->setPosition((winsize.width-menuItem1->getContentSize().width)/2,
			               winsize.height - (menuItem1->getContentSize().height + MENU_Y_PAD)*2 - TITLE_HEIGHT);
	menuItem2->setLevelInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTaskLevel"), task2_star_num / 20)->getCString());
	menuItem2->setInfoTitle(LocalizedStringGetter::getStringByKey("AchieveTask2"));
	menuItem2->setInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTask2Dsc"), task2_star_num)->getCString());
	menuItem2->setProgressState(star_level_num ,task2_star_num);
	menuItem2->setRewardDiamond(DIAMOND_REWARD);


	auto menuItemBack = AchievementMenuItem::create("WinBackButton.png", "WinBackButton.png","WinBackButton.png",
			CC_CALLBACK_1(AchievementView::menuCallback, this));
	menuItemBack->setTag(MarketBack);
	menuItemBack->setAnchorPoint(Vec2::ZERO);
	menuItemBack->setPosition(Point(winsize.width - 50,winsize.height - 50));

    auto menu = Menu::create(menuItem1,menuItem2, NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    addChild(menu,ZMenu);

    auto menuBack = Menu::create(menuItemBack, NULL);
    menuBack->setAnchorPoint(Vec2::ZERO);
    menuBack->setPosition(Vec2::ZERO);
    addChild(menuBack,ZMenuBack);

}

void AchievementView::titleInit()
{
	Size winsize = Director::getInstance()->getWinSize();

	__String *fileName = __String::createWithFormat("ShopTitleBg.png");
	auto title_bg = Sprite::createWithSpriteFrameName(fileName->getCString());
	title_bg->setAnchorPoint(Vec2::ZERO);
	title_bg->setPosition(Point(0, winsize.height - 4*CELL_HEIGHT));
	this->addChild(title_bg);

	__String *healthStr = __String::create(LocalizedStringGetter::getStringByKey("AchieveString"));
	LabelTTF* titleLabel = LabelTTF::create(healthStr->getCString(), "Arial", 24);
	titleLabel->setPosition(Point(winsize.width / 2, winsize.height - 2*CELL_HEIGHT));
	titleLabel->setColor(Color3B::WHITE);
	this->addChild(titleLabel);
}

void AchievementView::menuCallback(Ref *sender)
{
	AchievementMenuItem *seletedMenu = (AchievementMenuItem*)sender;

	if(seletedMenu->getTag() == MarketMenu1){
		if(task1_level_num <= passnum){
			task1_level_num += 20;
			UserDefault::getInstance()->setIntegerForKey(TASK1_LEVEL_NUM_KEY, task1_level_num);
			seletedMenu->setLevelInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTaskLevel"), task1_level_num / 20)->getCString());
			seletedMenu->setInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTask1Dsc"), task1_level_num)->getCString());
			seletedMenu->setProgressState(passnum ,task1_level_num);
			GoldSaveModel::getInstance()->changeDiamondNum(DIAMOND_REWARD);
		}
	} else if(seletedMenu->getTag() == MarketMenu2){
		if(task2_star_num <= star_level_num){
			task2_star_num += 20;
			UserDefault::getInstance()->setIntegerForKey(TASK2_STAR_NUM_KEY, task2_star_num);
			seletedMenu->setLevelInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTaskLevel"), task2_star_num / 20)->getCString());
			seletedMenu->setInfo(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveTask2Dsc"), task2_star_num)->getCString());
			seletedMenu->setProgressState(star_level_num ,task2_star_num);
			GoldSaveModel::getInstance()->changeDiamondNum(DIAMOND_REWARD);
		}
	} else if(seletedMenu->getTag() == MarketBack){
		Director::getInstance()->popScene();
	}
}

void AchievementView::bgInit()
{
    auto bg = Sprite::create("DiamondScreenBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    this->addChild(bg);
}
